/**
 * ...
 * @author Krzysztof Rozalski
 */

package hapi.facebook;
import hapi.facebook.externs.FacebookApiException;
import hapi.log.LogLevel;
import haxe.Template;

class FacebookHelper {

	var adapter:FacebookAdapter;
	static var inst:FacebookHelper;
	
	function new(adapter:FacebookAdapter) {
		this.adapter = adapter;
	}
	
	public static function registerAdapter(adapter) {
		inst = new FacebookHelper(adapter);
	}
	
	public static function getInstance():FacebookHelper {
		if ( inst == null ) throw "FacebookHelper is not initialized";
		return inst;
	}
	
	public function getFriendsCount(playerId:String):{ count:Int, playingGame:Int } {
		var query = "SELECT uid, has_added_app FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = " + playerId + ")";
		try {
			var result:Hash<Dynamic> = adapter.fql(query);
			var count = 0;
			var countPlaying = 0;
			for ( key in result.keys() ) {
				++count;
				var row = result.get(key);
				if (row.get("has_added_app")) ++countPlaying;
				trace(count+":"+row.get("uid")+"|"+row.get("has_added_app"));
			}
			return { count:count, playingGame:countPlaying };
		}catch (e:FacebookApiException) {
			trace("error on getFriendsCount for " + playerId, LogLevel.error);
			trace(e, LogLevel.error);
		}
		return null;
	}
	
	public function getFriendsUids(playerId:String, onlyAppUsers:Bool=true):List<String> {
		var query = "SELECT uid FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = " + playerId + ")";
		if (onlyAppUsers) query = query + " and is_app_user";
		try {
			var result:Hash<Dynamic> = adapter.fql(query);
			var count = 0;
			var countPlaying = 0;
			var list = new List<String>();
			for ( key in result.keys() ) {
				var row = result.get(key);
				list.add(row.get("uid"));
			}
			return list;
		}catch (e:FacebookApiException) {
			trace("error on getFriendsUids for " + playerId, LogLevel.error);
			trace(e, LogLevel.error);
		}
		return null;
	}
	
	public function getPlayerStats(playerId:String): FacebookUserBasicStats {
		var friendsQuery = FacebookQuries.countFriends(playerId);
		var isFanQuery = FacebookQuries.isFanOf(playerId, adapter.config.appId);
		try {
			var queryResult:Hash<Dynamic> = adapter.fqlMultiquery([friendsQuery, isFanQuery]);
			var result = { friendsCount:0, friendsPlayingCount:0, isFan:false };
			setFriendsCount(queryResult.get("0").get("fql_result_set"), result);
			markIfIsFan(queryResult.get("1").get("fql_result_set").get("0"), result);
			return result;
		}catch (e:FacebookApiException) {
			trace("error on getFriendsCount for " + playerId, LogLevel.error);
			trace(e, LogLevel.error);
		}
		return null;
	}
	
	function setFriendsCount(queryResult:Hash<Dynamic>, result:FacebookUserBasicStats):Void {
		var count = 0;
		var countPlaying = 0;
		for ( key in queryResult.keys() ) {
			++count;
			var row = queryResult.get(key);
			if (row.get("has_added_app")) ++countPlaying;
		}
		result.friendsCount = count;
		result.friendsPlayingCount = countPlaying;
	}
	
	function markIfIsFan(queryResult:Hash<Dynamic>, result:FacebookUserBasicStats ):Void {
		if ( queryResult == null || !queryResult.exists("page_id") ) result.isFan = false;
		else result.isFan = true;
	}
}